import { Controller, Delete, Get, HttpException, HttpStatus, Inject, Logger, LoggerService, Patch, Post } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.entity';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

@Controller('user')
export class UserController {
    // private logger = new Logger(UserController.name)
    constructor(
        private userServie: UserService,
        @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService
    ) {
        this.logger.log('UserController init')
    }
    @Get()
    getUsers(): any {
        // const user = { isAdmin: false }
        // if (!user.isAdmin){
        //     throw new HttpException('user is not admin,Forbidden', HttpStatus.FORBIDDEN);
        // }
        this.logger.log('getUsers 请求成功')
        this.logger.warn('getUsers 请求成功')
        this.logger.error('getUsers 请求成功')
        this.logger.debug('getUsers 请求成功')
        this.logger.verbose('getUsers 请求成功')
        return this.userServie.findAll();
    }

    @Post()
    addUser(): any {
        // todo 解析 Body 参数
        const user = { username: 'admin', password: '123456' } as User;
        return this.userServie.create(user);
    }

    @Patch()
    updateUser(): any {
        // todo 传递参数id
        // todo 异常处理
        const user = { username: 'newname' } as User;
        return this.userServie.update(1, user);
    }

    @Delete()
    deleteUser(): any {
        // todo 传递参数id
        return this.userServie.remove(1);
    }

    @Get('/profile')
    getUserProfile(): any {
        return this.userServie.findProfile(2);
    }

    @Get('/logs')
    getUserLogs(): any {
        return this.userServie.findUserLogs(2);
    }

    @Get('logsByGroup')
    async getLogsByGroup(): Promise<any> {
        const res = await this.userServie.findLogsByGroup(2);
        return res.map((o) => ({
            result: o.result,
            count: o.count,
        }))
    }
}
